package com.zzz.restcomponent.ssh;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author zyl
 */
@Component
public class IOComponent {
    public final Session session;

    public IOComponent(Session session) {
        this.session = session;
    }

    public String get() throws IOException {
        InputStream stdout = session.getStdout();
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            byte[] buff = new byte[1024];
            int read = 0;
            while ((read = stdout.read(buff)) != -1) {
                byteArrayOutputStream.write(buff, 0, read);
            }
            return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            session.close();
        }
        return null;
    }

    public void add(String exec) throws IOException {
        session.execCommand(exec);
        try (InputStream in = new StreamGobbler(session.getStdout()); InputStream error = new StreamGobbler(session.getStderr())) {
            while (true) {
                String s = processStream(in, "UTF-8");
                String s1 = processStream(error, "UTF-8");
                System.out.println(s);
                System.out.println(s1);
                OutputStream stdin = session.getStdin();
                stdin.write(exec.getBytes());
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    public void exec() throws IOException {
        session.requestPTY("bash");
        session.startShell();
        try (BufferedReader readOut = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
             BufferedReader readError = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStderr())))) {

            PrintWriter printWriter = new PrintWriter(session.getStdin());

            while (true) {

                printWriter.println("cd ~ && ls -li");
                printWriter.println("exit");
                session.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 3000);
                new Thread(() -> {
                    while (true) {
                        String s = null;
                        try {
                            s = readOut.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (s == null) {
                            break;
                        }
                        System.out.println(">>>>>" + s);
                    }
                    while (true) {
                        String s = null;
                        try {
                            s = readError.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (s == null) {
                            break;
                        }
                        System.out.println(">>>>>" + s);
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("ver");


    }

    private String processStream(InputStream in, String charset) throws Exception {
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        while (in.read(buf) != -1) {
            sb.append(new String(buf, charset));
        }
        return sb.toString();
    }

    @PostConstruct
    public void test() throws IOException {
        System.out.println("asd");

    }
}
